﻿using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace 控件测试
{

    //MainWindow主窗体的整个继承路线:MainWindow->Window->ContentControl->Control->FrameworkElement->UIElement->Visual->DependencyObject->DispatcherObject
    public partial class MainWindow : Window, INotifyPropertyChanged
    {

        private string file = string.Empty;


        public MainWindow()
        {
            InitializeComponent();
            //创建窗体源时引发此事件
            this.SourceInitialized += (s, e) => Trace.WriteLine("1.MainWindow的SourceInitialized被执行。。。");
            //当前窗体成为前台窗体时引发此事件
            this.Activated += (s, e) => Trace.WriteLine("2.MainWindow的Activated被执行。。。");
            //当前窗体内部所有元素完成布局和呈现时引发此事件
            this.Loaded += (s, e) => Trace.WriteLine("3.MainWindow的Loaded被执行。。。");
            //当前窗体的内容呈现之后引发此事件
            this.ContentRendered += (s, e) => Trace.WriteLine("4.MainWindow的ContentRendered被执行。。。");
            //当前窗体成为后台窗体时引发此事件
            this.Deactivated += (s, e) => Trace.WriteLine("5.MainWindow的Deactivated被执行。。。");
            //当前窗体关闭之前引发此事件
            this.Closing += (s, e) => Trace.WriteLine("6.MainWindow的Closing被执行。。。");
            //当前窗体关闭之后引发此事件
            this.Closed += (s, e) => Trace.WriteLine("7.MainWindow的Closed被执行。。。");
            //当前窗体从元素树中删除时引发此事件
            this.Unloaded += (s, e) => Trace.WriteLine("8.MainWindow的Unloaded被执行。。。");


            Task.Run(() =>
            {
                Task.Delay(5000).Wait();
                //使用Dispatcher跨线程访问UI
                Dispatcher.Invoke(() => { tb.Text = "跨线程访问UI"; });

            });


            DataContext = this;//将当前窗体作为ViewModel赋值给当前窗体的DataContext
            Loaded += (s, e) =>
            {
                //计算滚动条的最大值
                Maximum = element.ActualWidth - viewport.ActualWidth;
            };


            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(1000);
            timer.Tick += (s, e) =>
            {
                var ts = _MediaElement.Position;
                _ProgressBar.Value = ts.TotalMilliseconds;//更新当前播放进度

            };
            timer.Start();


            listbox.Items.Add(new Person { Name = "张三", Age = 22, Address = "广东省廉江市车板镇大坝村" });
            listbox.Items.Add(new Person { Name = "李四", Age = 23, Address = "江西省景德镇市市辖区" });
            listbox.Items.Add(new Person { Name = "王五", Age = 24, Address = "上海市虹口区" });


            listview.Items.Add(new Person { Name = "张三", Age = 22, Address = "广东省廉江市车板镇大坝村" });
            listview.Items.Add(new Person { Name = "李四", Age = 23, Address = "江西省景德镇市市辖区" });
            listview.Items.Add(new Person { Name = "王五", Age = 24, Address = "上海市虹口区" });


            List<Person> list = new List<Person>();

            list.Add(new Person { Name = "张三", Age = 22, Address = "广东省廉江市车板镇大坝村" });
            list.Add(new Person { Name = "李四", Age = 23, Address = "江西省景德镇市市辖区" });
            list.Add(new Person { Name = "王五", Age = 24, Address = "上海市虹口区" });

            datagrid.ItemsSource = list;

            combobox2.ItemsSource = list;
        }

        private void exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        int count = 0;
        private void RepeatButton_Click(object sender, RoutedEventArgs e)
        {
            count++;
            log.Text = "执行" + count;
        }

        private void _button_Click(object sender, RoutedEventArgs e)
        {
          
                TextRange textRange = new TextRange(_richTextBox.Document.ContentStart, _richTextBox.Document.ContentEnd);
                MessageBox.Show(textRange.Text);

                Paragraph paragraph = new Paragraph();
                Run run = new Run($"当前时间：{DateTime.Now}"); //手动加换行
                run.Foreground = Brushes.Black;
                paragraph.Inlines.Add(run);
                _richTextBox.Document.Blocks.Add(paragraph);
          
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Process.Start("https://www.baidu.com", "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
        }


        private double maximum = 0;
        /// <summary>
        /// 滚动条的最大值
        /// </summary>
        public double Maximum
        {
            get { return maximum; }
            set { maximum = value; NotifyPropertyChanged("Maximum"); }
        }

        private double x = 0;
        /// <summary>
        /// 滚动条的当前值
        /// </summary>
        public double X
        {
            get { return x; }
            set { x = value; CanvasLeft = -x; NotifyPropertyChanged("X"); }
        }

        private double canvasLeft = 0;
        /// <summary>
        /// 相对于Canvas控件Left边距
        /// </summary>
        public double CanvasLeft
        {
            get { return canvasLeft; }
            set { canvasLeft = value; NotifyPropertyChanged("CanvasLeft"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 属性通知方法
        /// </summary>
        /// <param name="propertyName"></param>
        protected virtual void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }


        private void OpenMedia(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = "视频文件 (.mp4)|*.mp4",
                Multiselect = true
            };
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
                file = openFileDialog.FileName;
                _MediaElement.MediaOpened += _MediaElement_MediaOpened;
                _MediaElement.Source = new System.Uri(file);
                this.Title = file;
                _TextBlock.Text = file;
            }
        }

        private void _MediaElement_MediaOpened(object sender, RoutedEventArgs e)
        {
            if (_MediaElement.NaturalDuration.HasTimeSpan)
            {
                var ts = _MediaElement.NaturalDuration.TimeSpan;
                _ProgressBar.Maximum = ts.TotalMilliseconds;//设置播放进度条总长
            }
        }

        private void PlayMedia(object sender, RoutedEventArgs e)
        {
            _MediaElement.Play();//播放
            _Border.Visibility = Visibility.Collapsed;

        }

        private void StopMedia(object sender, RoutedEventArgs e)
        {
            _MediaElement.Pause();//暂停
        }

        private void BackMedia(object sender, RoutedEventArgs e)
        {
            _MediaElement.Position = _MediaElement.Position - TimeSpan.FromSeconds(10);//快退10秒
        }

        private void ForwardMedia(object sender, RoutedEventArgs e)
        {
            _MediaElement.Position = _MediaElement.Position + TimeSpan.FromSeconds(10);//快进10秒

        }

        private void _Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            _MediaElement.Volume = _Slider.Value;//设置音量
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var selectedItem = listbox.SelectedItem;
            var selectedValue = listbox.SelectedValue;
            _TextBlock2.Text = $"{selectedItem},{selectedValue}";
        }

        private void Button_Click2(object sender, RoutedEventArgs e)
        {
            try
            {
                var selectedItem = listbox2.SelectedItem;
                var content = ((ContentControl)selectedItem).Content;
                textblock3.Text = $"selectedItem={selectedItem}\r\ncontent={content}";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void listview_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListView listView = sender as ListView;
            if (listView == null) return;

            var person = listView.SelectedItem as Person;
            if (person == null) return;

            _TextBlockName.Text = person.Name;
            _TextBlockAge.Text = person.Age + "岁";
            _TextBlockAddress.Text = person.Address;
        }

        private void datagrid_Selected(object sender, SelectionChangedEventArgs e)
        {
            DataGrid datagrid = sender as DataGrid;
            if (datagrid == null) return;

            var person = datagrid.SelectedItem as Person;
            if (person == null) return;

            _TextBlockName.Text = person.Name;
            _TextBlockAge.Text = person.Age + "岁";
            _TextBlockAddress.Text = person.Address;
        }


        private void combobox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            _TextBlockTel.Text = combobox1.Text;
        }

        private void combobox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox combobox = sender as ComboBox;
            if (combobox == null) return;

            var person = combobox.SelectedItem as Person;
            if (person == null) return;

            _TextBlockName1.Text = person.Name;
            _TextBlockAge1.Text = person.Age + "岁";
            _TextBlockAddress1.Text = person.Address;
        }
    }


    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }

    }
}